import { Http, Util } from '@ibizstudio/runtime';
import { FetchService } from '@ibizstudio/runtime';

/**
 * 粘贴板控制器
 *
 * @export
 * @class ModelService
 */
export class ModelService {
  /**
   * 唯一实例
   *
   * @private
   * @static
   * @type {ModelService}
   * @memberof ModelService
   */
  private static readonly instance: ModelService = new ModelService();

  /**
   * http请求对象
   *
   * @protected
   * @type {Http}
   * @memberof ModelService
   */
  protected http: Http = Http.getInstance();

  /**
   * Creates an instance of ModelService.
   * @memberof ModelService
   */
  constructor() {
    if (ModelService.instance) {
      return ModelService.instance;
    }
  }

  /**
   * 加载模型
   *
   * @param {{ srfdename: string, srfkey: string, srfparentdename?: string, srfparentkey?: string }} params
   * @returns {Promise<any>}
   * @memberof ModelService
   */
  loadModel(params: { srfdename: string; srfkey: string; srfparentdename?: string; srfparentkey?: string }): Promise<any> {
    const deNamePlural = Util.srfpluralize(params.srfdename).toLowerCase();
    return FetchService.getInstance().post(`/${deNamePlural}/${params.srfkey}/exportModel`, {});
  }

  /**
   * 保存模型
   *
   * @param {*} params
   * @returns {Promise<any>}
   * @memberof ModelService
   */
  saveModel(model: any, params: any): Promise<any> {
    const deNamePlural = Util.srfpluralize(params.srfdename).toLowerCase();
    return FetchService.getInstance().post(`/${deNamePlural}/importModel`, model);
  }

  /**
   * 获取实例
   *
   * @static
   * @returns {ModelService}
   * @memberof ModelService
   */
  static getInstance(): ModelService {
    return this.instance;
  }
}
